#pip install fastapi uvicorn
from fastapi import FastAPI
from pydantic import BaseModel
import uvicorn

from data.SentimentPredictor import SentimentPredictor
# 导入上面的SentimentPredictor类

app = FastAPI(title="情感分类API")
# 初始化预测器（启动时加载一次，避免重复加载）
predictor = SentimentPredictor(
    model_path=r"params\1bert.pth",
    bert_path=r"D:\AI\model\bert-base-chinese"
)


# 定义请求体模型
class TextInput(BaseModel):
    text: str
@app.post("/predict")
def predict_sentiment(input_data: TextInput):
    """接收文本，返回情感分类结果"""
    result = predictor.predict(input_data.text)
    return {"text": input_data.text, "sentiment": result}

if __name__ == "__main__":
    # 启动服务，默认端口8000
    uvicorn.run(app, host="0.0.0.0", port=8000)